package tman.main;


import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.TreeSet;

import org.jfree.ui.*;
import org.jfree.chart.*;
import org.jfree.data.category.*;
import org.jfree.chart.plot.PlotOrientation;

public class GraphPanel3 extends ApplicationFrame {
	
	final JFreeChart chart;
	public JFreeChart getChart()
	{
		return chart;
	}
        public GraphPanel3(final String title, HashMap<Integer, HashMap<Integer, Integer>> DetailedHopCountDistr) {
                super(title);
                final CategoryDataset dataset = createDataset(DetailedHopCountDistr);
                chart = createChart(dataset);
                final ChartPanel chartPanel = new ChartPanel(chart);
                chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
                setContentPane(chartPanel);
        }

        private CategoryDataset createDataset(HashMap<Integer, HashMap<Integer, Integer>> DetailedHopCountDistr) {
                DefaultCategoryDataset result = new DefaultCategoryDataset();
                Collection<Integer> keys = DetailedHopCountDistr.keySet();
                ArrayList<Integer> sortedKeys = new ArrayList(new TreeSet(keys));
                for(int i=0; i<sortedKeys.size();i++)
                {
                	Collection<Integer> otherKeys = DetailedHopCountDistr.get(sortedKeys.get(i)).keySet();
                    ArrayList<Integer> otherSortedKeys = new ArrayList(new TreeSet(otherKeys));
                    for(int j=0; j<otherSortedKeys.size(); j++)
                    {
                    	String label = "OD=" + otherSortedKeys.get(j);
                    	result.addValue(DetailedHopCountDistr.get(sortedKeys.get(i)).get(otherSortedKeys.get(j)), label, sortedKeys.get(i));
                    }
                }
                return result;
        }

        private JFreeChart createChart(final CategoryDataset dataset) {
                final JFreeChart chart = ChartFactory.createStackedBarChart(
                                "HopCount Frequencies", "Hop-Count", "Frequency", dataset,
                                PlotOrientation.VERTICAL, true, true, false);
                return chart;
        }
}